(function (angular) {
    'use strict';

    angular.module('ytong.controllers').controller('authorizeCtrl',authorizeCtrl);

    function authorizeCtrl(authorizationManager, ModalService, Constants, feedbacksManager){
        this.users = authorizationManager.getUnauthorizedUsers();

        this.selectUser = function(user){

            ModalService.showModal({
                templateUrl: 'authorizeUser',
                controller: 'authorizeUserController as auc',
                inputs:{
                    user: user
                }
            })
                .then(function(modal){
                    modal.element.modal();
                    modal.close.then(function(result){

                        if(result.action === Constants.COMMANDS.UPDATE){
                            authorizationManager.authorizeUser(result.user.id,result.user.role);
                        }
                    });
                });
        };

    };

    angular.module('ytong.controllers').controller('authorizeUserController', authorizeUserController);

    function authorizeUserController(close, user){

        this.selectedUser = user;
        this.selectedUser.role = "";

        this.close = function(action) {
            var result = {};
            result.action = action;
            result.user = this.selectedUser;
            close(result, 500); // close, but give 500ms for bootstrap to animate
        };

    };


}(angular));